﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace MvcEngine.Mvc.Filters
{
    public class RedirectToUrlOnErrorAttribute : RedirectOnErrorAttribute
    {
        #region Public properties
        public string Url { get; set; } 
        #endregion

        #region Protected methods
        protected override bool Validate(ExceptionContext exceptionContext)
        {
            if (string.IsNullOrEmpty(Url) || Url.Trim() == string.Empty)
                throw new ArgumentNullException("RedirectToUrlOnErrorAttribute's Url property must have a value.");
            return true;
        }

        protected override void Redirect(ExceptionContext exceptionContext)
        {
            exceptionContext.ExceptionHandled = true;
            exceptionContext.HttpContext.Response.Redirect(Url, true);
        } 
        #endregion
    }
}
